package net.xgclass.online_xgclass.controller;

import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.xgclass.online_xgclass.model.entity.Chapter;
import net.xgclass.online_xgclass.model.entity.Episode;
import net.xgclass.online_xgclass.model.entity.Video;
import net.xgclass.online_xgclass.model.entity.VideoBanner;
import net.xgclass.online_xgclass.service.VideoService;
import net.xgclass.online_xgclass.utils.GetPlayUrlUtils;
import net.xgclass.online_xgclass.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("api/v1/pub/video")
public class VideoController {


    @Autowired
    private VideoService videoService;


    /**
     * 轮播图列表
     * @return
     */
    @GetMapping("list_banner")
    public JsonData indexBanner(){


        List<VideoBanner> bannerList =  videoService.listBanner();



        return JsonData.buildSuccess(bannerList);

    }


    /**
     * 视频列表
     * @return
     */
    @RequestMapping("list")
    public JsonData listVideo(){

        List<Video> videoList = videoService.listVideo();
        return JsonData.buildSuccess(videoList);
    }


    /**
     * 查询视频详情，包含章，集信息
     * @param videoId
     * @return
     */
    @GetMapping("find_detail_by_id")
    public JsonData findDetailById(@RequestParam(value = "video_id",required = true)int videoId){




        Video video = videoService.findDetailById(videoId);

        return JsonData.buildSuccess(video);

    }

    //******************************后台****************************************//

    /**
     * 查询视频章节详细信息
     * @return
     */
    @RequestMapping(
            value = {"listVideo"},
            method = {RequestMethod.GET}
    )
    public JsonData listVideoDetail(){
        List<Video> videos = videoService.listVideoDetail();
        return videos ==null?JsonData.buildError("查询视频章节信息失败"):JsonData.buildSuccess(videos);
    }

    /**
     * 查询视频信息  后台
     * @param pagenum
     * @param pagesize
     * @return
     */
    @RequestMapping(
            value = {"videoPageInfo"},
            method = {RequestMethod.GET}
    )
    public PageInfo<Video> videoPageInfo(int pagenum,int pagesize){
        PageHelper.startPage(pagenum,pagesize);
        List<Video> videos = videoService.videoPageInfo(pagenum,pagesize);
        PageInfo<Video> pageInfo = new PageInfo<>(videos);
        return pageInfo;

    }


    /**
     * 上传图片
     * @param file
     * @return
     */
    @RequestMapping(
            value = {"uploadImg"},
            method = {RequestMethod.POST}
    )
    public JsonData uploadOssFile(@RequestParam("file") MultipartFile file){
        String url = videoService.uploadFileImg(file);
        return url ==null ? JsonData.buildError("上图图片失败"):JsonData.buildSuccess(url);
    }

    /**
     * 上传视频信息
     * @param video
     * @return
     */
    @RequestMapping(
            value = {"addVideo"},
            method = {RequestMethod.POST}
    )
    public JsonData addVideo(@RequestBody Video video){
        int  videoId = videoService.addVideo(video);
        return JsonData.buildSuccess(videoId);
    }

    /**
     * 通过视频id查询章节信息 后台
     * @param videoId
     * @return
     */
    @RequestMapping(
            value = {"queryChaEpiByVideoId"},
            method = {RequestMethod.GET}
    )
    public JsonData queryChaEpiByVideoId(@RequestParam Integer videoId){
        List<Chapter> chapterList = videoService.queryChaEpiByVideoId(videoId);
        return chapterList !=null?JsonData.buildSuccess(chapterList):JsonData.buildError("查询章节信息失败");
    }

    /**
     * 上传视频并获取视频地址  后台
     * 先获取阿里云视频点播的视频id，再通过id获取视频播放地址
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping(
            value = {"getPlayUrl"},
            method = {RequestMethod.POST}
    )
    public JsonData uploadVideo(@RequestParam("file") MultipartFile file) throws Exception {
        String id = videoService.uploadVideo(file);
        List<GetPlayInfoResponse.PlayInfo> playUrl = GetPlayUrlUtils.getPlayUrl(id);
        return playUrl !=null ?JsonData.buildSuccess(playUrl):JsonData.buildError("获取视频地址失败");
    }

    /**
     * 添加章  后台
     * @param chapter
     * @return
     */
    @RequestMapping(
            value = {"addChapter"},
            method = {RequestMethod.POST}
    )
    public JsonData addChapter(@RequestBody Chapter chapter){
        int flag = videoService.addChapter(chapter);
        return flag ==1 ?JsonData.buildSuccess():JsonData.buildError("添加章失败");

    }

    /**
     * 添加集  后台
     * @param episode
     * @return
     */
    @RequestMapping(
            value = {"addEpisode"},
            method = {RequestMethod.POST}
    )
    public JsonData addEpisode(@RequestBody Episode episode){
        int flag = videoService.addEpisode(episode);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("添加集失败");
    }

    /**
     * 查询轮播图列表 分页 后台
     * @param pagenum
     * @param pagesize
     * @return
     */
    @RequestMapping(
            value = {"videoBanner"},
            method = {RequestMethod.GET}
    )
    public PageInfo<VideoBanner> videoBannerPageInfo(int pagenum,int pagesize){
        PageHelper.startPage(pagenum,pagesize);
        List<VideoBanner> videoBanners = videoService.videoBannerPageInfo(pagenum,pagesize);
        PageInfo<VideoBanner> videoBannerPageInfo = new PageInfo<>(videoBanners);
        return videoBannerPageInfo;
    }

    /**
     * 修改课程基本信息
     * @param video
     * @return
     */
    @RequestMapping(
            value = {"editCourse"},
            method = {RequestMethod.PUT}
    )
    public JsonData editCourse(@RequestBody Video video){
        int flag = videoService.editCourse(video);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("修改课程信息失败");
    }

    /**
     * 删除课程 章 集信息
     * @param id
     * @return
     */
    @RequestMapping(
            value = {"/{id}"},
            method = {RequestMethod.DELETE}
    )
    public JsonData removeCourse(@PathVariable Integer id){
        int flag = videoService.removeCourse(id);
        //根据videoId删除集
        videoService.removeChapter(id);
        //根据videoId删除集
        videoService.removeEpisode(id);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("删除课程失败");
    }

    /**
     * 修改章信息
     * @param chapter
     * @return
     */
    @RequestMapping(
            value = {"editChapter"},
            method = {RequestMethod.PUT}
    )
    public JsonData editChapter(@RequestBody Chapter chapter){
        int flag = videoService.editChapter(chapter);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("修改章失败");
    }

    /**
     * 修改集
     * @param episode
     * @return
     */
    @RequestMapping(
            value = {"editEpisode"},
            method = {RequestMethod.PUT}
    )
    public JsonData editEpisode(@RequestBody Episode episode){
        int flag = videoService.editEpisode(episode);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("修改集失败");
    }

    /**
     * 删除章 集信息
     * @param id
     * @return
     */
    @RequestMapping(
            value = {"/removeChapter/{id}"},
            method = {RequestMethod.DELETE}
    )
    public JsonData removeChapter(@PathVariable Integer id){
        int flag = videoService.removeChapterByChapterId(id);
        //根据chapterId删除集
        videoService.removeEpisodeByChapterId(id);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("删除章失败");
    }

    /**
     * 根据 EpisodeId 删除集
     * @param id
     * @return
     */
    @RequestMapping(
            value = {"/removeEpisode/{id}"},
            method = {RequestMethod.DELETE}
    )
    public JsonData removeEpisode(@PathVariable Integer id){
        int flag = videoService.removeEpisodeByEpisodeId(id);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("删除集失败");
    }

    /**
     * 增加轮播图
     * @param videoBanner
     * @return
     */
    @RequestMapping(
            value = {"addBanner"},
            method = {RequestMethod.POST}
    )
    public JsonData addBanner(@RequestBody VideoBanner videoBanner){
        int flag = videoService.addBanner(videoBanner);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("删除轮播图失败");

    }

    /**
     * 修改轮播图
     * @param videoBanner
     * @return
     */
    @RequestMapping(
            value = {"/editBanner"},
            method = {RequestMethod.PUT}
    )
    public JsonData editBanner(@RequestBody VideoBanner videoBanner){
        int flag = videoService.editBanner(videoBanner);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("修改轮播图失败");

    }

    /**
     * 删除轮播图
     * @param id
     * @return
     */
    @RequestMapping(
            value = {"/removeBanner/{id}"},
            method = {RequestMethod.DELETE}
    )
    public JsonData removeBanner(@PathVariable Integer id){
        int flag = videoService.removeBanner(id);
        return flag==1?JsonData.buildSuccess():JsonData.buildError("删除轮播图失败");

    }
}
